using System;
using System.Collections.Generic;
using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class HeadBlendSubMenu : Script
{
	public static UIMenu menu = new UIMenu("", TextSetting.hbs_Menu, new Point(0, -107));

	private static List<dynamic> faceIdList = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> mixList = new List<object>(new string[1] { string.Empty });

	private static List<dynamic> parentList = new List<object>(new string[2] { "False", "True" });

	private static UIMenuListItem shapeFirstItem;

	private static UIMenuListItem shapeSecondItem;

	private static UIMenuListItem shapeThirdItem;

	private static UIMenuListItem skinFirstItem;

	private static UIMenuListItem skinSecondItem;

	private static UIMenuListItem skinThirdItem;

	private static UIMenuListItem shapeMixItem;

	private static UIMenuListItem skinMixItem;

	private static UIMenuListItem thirdMixItem;

	private static UIMenuListItem parentItem;

	public HeadBlendSubMenu()
	{
		Main.pool.Add(menu);
		menu.OnListChange += OnListChange;
		menu.OnItemSelect += OnItemSelect;
		menu.OnMenuClose += OnMenuClose;
	}

	public static void InitHeadBlendSubMenu()
	{
		menu.Clear();
		faceIdList = new List<object>();
		mixList = new List<object>();
		Main.P1.skin.GetHeadBlendFaceNumber();
		for (int i = 0; i < Main.P1.skin.Head_Blend.faceNumber; i++)
		{
			faceIdList.Add(i);
		}
		for (double num = 0.0; num <= 1.0; num += 0.1)
		{
			mixList.Add(Math.Round(num, 1));
		}
		Skin.HeadBlend HeadBlend;
		if (DressingScript.Dressing == Main.Member.P2)
		{
			HeadBlend = Main.P2.skin.Head_Blend;
		}
		else
		{
			HeadBlend = Main.P1.skin.Head_Blend;
		}
		menu.SetBannerType(new UIResRectangle());
		shapeFirstItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeFirst, faceIdList, HeadBlend.shapeFirstID);
		shapeSecondItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeSecond, faceIdList, HeadBlend.shapeSecondID);
		shapeThirdItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeThird, faceIdList, HeadBlend.shapeThirdID);
		skinFirstItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinFirst, faceIdList, HeadBlend.skinFirstID);
		skinSecondItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinSecond, faceIdList, HeadBlend.skinSecondID);
		skinThirdItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinThird, faceIdList, HeadBlend.skinThirdID);
		shapeMixItem = new UIMenuListItem(TextSetting.hbs_ListItem_ShapeMix, mixList, mixList.FindIndex(n => n.ToString() == HeadBlend.shapeMix.ToString()));
		skinMixItem = new UIMenuListItem(TextSetting.hbs_ListItem_SkinMix, mixList, mixList.FindIndex(n => n.ToString() == HeadBlend.skinMix.ToString()));
		thirdMixItem = new UIMenuListItem(TextSetting.hbs_ListItem_ThirdMix, mixList, mixList.FindIndex(n => n.ToString() == HeadBlend.thirdMix.ToString()));
		parentItem = new UIMenuListItem(TextSetting.hbs_ListItem_Parent, parentList, Convert.ToInt32(HeadBlend.isParent));
		menu.AddItem(shapeFirstItem);
		menu.AddItem(shapeSecondItem);
		menu.AddItem(shapeThirdItem);
		menu.AddItem(skinFirstItem);
		menu.AddItem(skinSecondItem);
		menu.AddItem(skinThirdItem);
		menu.AddItem(shapeMixItem);
		menu.AddItem(skinMixItem);
		menu.AddItem(thirdMixItem);
		menu.AddItem(parentItem);
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Randomize));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Clear));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_Distance));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnLeft));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_TurnRight));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleUp));
		menu.AddItem(new UIMenuItem(TextSetting.generic_MenuItem_AngleDown));
		menu.RefreshIndex();
	}

	private static void UpdateHeadBlendValue()
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			Main.P2.skin.Head_Blend.shapeFirstID = shapeFirstItem.Index;
			Main.P2.skin.Head_Blend.shapeSecondID = shapeSecondItem.Index;
			Main.P2.skin.Head_Blend.shapeThirdID = shapeThirdItem.Index;
			Main.P2.skin.Head_Blend.skinFirstID = skinFirstItem.Index;
			Main.P2.skin.Head_Blend.skinSecondID = skinSecondItem.Index;
			Main.P2.skin.Head_Blend.skinThirdID = skinThirdItem.Index;
			Main.P2.skin.Head_Blend.shapeMix = (float)mixList[shapeMixItem.Index];
			Main.P2.skin.Head_Blend.skinMix = (float)mixList[skinMixItem.Index];
			Main.P2.skin.Head_Blend.thirdMix = (float)mixList[thirdMixItem.Index];
			Main.P2.skin.Head_Blend.isParent = ((parentItem.Index != 0) ? true : false);
		}
		else
		{
			Main.P1.skin.Head_Blend.shapeFirstID = shapeFirstItem.Index;
			Main.P1.skin.Head_Blend.shapeSecondID = shapeSecondItem.Index;
			Main.P1.skin.Head_Blend.shapeThirdID = shapeThirdItem.Index;
			Main.P1.skin.Head_Blend.skinFirstID = skinFirstItem.Index;
			Main.P1.skin.Head_Blend.skinSecondID = skinSecondItem.Index;
			Main.P1.skin.Head_Blend.skinThirdID = skinThirdItem.Index;
			Main.P1.skin.Head_Blend.shapeMix = (float)mixList[shapeMixItem.Index];
			Main.P1.skin.Head_Blend.skinMix = (float)mixList[skinMixItem.Index];
			Main.P1.skin.Head_Blend.thirdMix = (float)mixList[thirdMixItem.Index];
			Main.P1.skin.Head_Blend.isParent = ((parentItem.Index != 0) ? true : false);
			Main.Clone.skin.Head_Blend = Main.P1.skin.Head_Blend;
		}
	}

	private void OnListChange(UIMenu sender, UIMenuListItem list, int index)
	{
		if (DressingScript.Dressing == Main.Member.P2)
		{
			SkinHelper.SetPedHeadBlendData(Main.P2.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
			Main.P2.skin.Head_Blend.action = true;
			return;
		}
		SkinHelper.SetPedHeadBlendData(Main.P1.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
		SkinHelper.SetPedHeadBlendData(Main.Clone.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
		Main.P1.skin.Head_Blend.action = true;
		Main.Clone.skin.Head_Blend.action = true;
	}

	private void OnItemSelect(UIMenu sender, UIMenuItem item, int index)
	{
		switch (index)
		{
		case 10:
		{
			Random random = new Random(Guid.NewGuid().GetHashCode());
			shapeFirstItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
			shapeSecondItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
			shapeThirdItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
			skinFirstItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
			skinSecondItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
			skinThirdItem.Index = random.Next(0, Main.P1.skin.Head_Blend.faceNumber);
			shapeMixItem.Index = random.Next(0, mixList.Count);
			skinMixItem.Index = random.Next(0, mixList.Count);
			thirdMixItem.Index = random.Next(0, mixList.Count);
			if (DressingScript.Dressing == Main.Member.P2)
			{
				SkinHelper.SetPedHeadBlendData(Main.P2.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
				Main.P2.skin.Head_Blend.action = true;
				break;
			}
			SkinHelper.SetPedHeadBlendData(Main.P1.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
			SkinHelper.SetPedHeadBlendData(Main.Clone.Character, shapeFirstItem.Index, shapeSecondItem.Index, shapeThirdItem.Index, skinFirstItem.Index, skinSecondItem.Index, skinThirdItem.Index, (float)mixList[shapeMixItem.Index], (float)mixList[skinMixItem.Index], (float)mixList[thirdMixItem.Index], Convert.ToBoolean(parentList[parentItem.Index]));
			Main.P1.skin.Head_Blend.action = true;
			Main.Clone.skin.Head_Blend.action = true;
			break;
		}
		case 11:
		{
			UIMenuListItem uIMenuListItem = shapeFirstItem;
			UIMenuListItem uIMenuListItem2 = shapeSecondItem;
			UIMenuListItem uIMenuListItem3 = shapeThirdItem;
			UIMenuListItem uIMenuListItem4 = skinFirstItem;
			UIMenuListItem uIMenuListItem5 = skinSecondItem;
			UIMenuListItem uIMenuListItem6 = skinThirdItem;
			UIMenuListItem uIMenuListItem7 = shapeMixItem;
			UIMenuListItem uIMenuListItem8 = skinMixItem;
			UIMenuListItem uIMenuListItem9 = thirdMixItem;
			int num = (parentItem.Index = 0);
			int num3 = (uIMenuListItem9.Index = num);
			int num5 = (uIMenuListItem8.Index = num3);
			int num7 = (uIMenuListItem7.Index = num5);
			int num9 = (uIMenuListItem6.Index = num7);
			int num11 = (uIMenuListItem5.Index = num9);
			int num13 = (uIMenuListItem4.Index = num11);
			int num15 = (uIMenuListItem3.Index = num13);
			int index2 = (uIMenuListItem2.Index = num15);
			uIMenuListItem.Index = index2;
			if (DressingScript.Dressing == Main.Member.P2)
			{
				Main.P2.skin.Head_Blend = default(Skin.HeadBlend);
				SkinHelper.SetPedHeadBlendData(Main.P2.Character, 0, 0, 0, 0, 0, 0, 0f, 0f, 0f, isParent: false);
				break;
			}
			Main.P1.skin.Head_Blend = default(Skin.HeadBlend);
			SkinHelper.SetPedHeadBlendData(Main.P1.Character, 0, 0, 0, 0, 0, 0, 0f, 0f, 0f, isParent: false);
			Main.Clone.skin.Head_Blend = default(Skin.HeadBlend);
			SkinHelper.SetPedHeadBlendData(Main.Clone.Character, 0, 0, 0, 0, 0, 0, 0f, 0f, 0f, isParent: false);
			break;
		}
		case 12:
			DressingScript.Distance();
			break;
		case 13:
			DressingScript.TurnLeft();
			break;
		case 14:
			DressingScript.TurnRight();
			break;
		case 15:
			DressingScript.AngleUp();
			break;
		case 16:
			DressingScript.AngleDown();
			break;
		}
	}

	private void OnIndexChange(UIMenu sender, int index)
	{
	}

	private void OnMenuClose(UIMenu sender)
	{
		UpdateHeadBlendValue();
		DressingRoomAdvancedMenu.CheckCanUseMenuItem();
		menu.Visible = false;
		menu.RefreshIndex();
	}
}
